package com.ztools.pc.service.impl.store;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.store.Commodity;
import com.ztools.common.model.pc.store.StoreStock;
import com.ztools.common.model.pc.store.StoreStockDetail;
import com.ztools.common.util.common.PageUtil;
import com.ztools.common.util.pc.DictUtil;
import com.ztools.pc.mapper.store.StoreStockDetailMapper;
import com.ztools.pc.service.store.CommodityService;
import com.ztools.pc.service.store.StoreStockDetailService;
import com.ztools.pc.service.store.StoreStockService;
import com.ztools.pc.service.sys.DictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author seeTools
 * @since 2023-05-28
 */
@Service
public class StoreStockDetailServiceImpl extends ServiceImpl<StoreStockDetailMapper, StoreStockDetail> implements StoreStockDetailService {

    @Resource
    private CommodityService commodityService;
    @Resource
    private StoreStockService storeStockService;
    @Resource
    private DictService dictService;


    /**
     * 分页查询
     *
     * @param pageUtil 分页数据
     * @return 对象
     */
    @Override
    public Page<StoreStockDetail> findList(PageUtil<StoreStockDetail> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<StoreStockDetail> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<StoreStockDetail> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        Page<StoreStockDetail> page = page(pages, queryWrapper);
        dictionary(page.getRecords());
        return page;
    }

    @Override
    public List<StoreStockDetail> findByObject(StoreStockDetail storeStockDetail) {
        QueryWrapper<StoreStockDetail> queryWrapper = new QueryWrapper<>(storeStockDetail);
        return list(queryWrapper);
    }

    @Override
    public boolean update(StoreStockDetail storeStockDetail) {
        boolean b = false;
        // 修改前先获取，在删除（乐观锁）
        StoreStockDetail byId = getById(storeStockDetail);
        b = updateById(storeStockDetail);
        return b;
    }

    @Override
    public boolean delete(StoreStockDetail storeStockDetail) {
        boolean b = false;
        StoreStockDetail byId = getById(storeStockDetail);
        b = removeById(storeStockDetail);
        return b;
    }

    @Override
    public boolean add(StoreStockDetail storeStockDetail) {
        StoreStockDetail byId = getById(storeStockDetail);
        Commodity commodity = commodityService.getById(storeStockDetail.getCommodityId());
        storeStockDetail.setCostPrice(commodity.getCostPrice());
        storeStockDetail.setTotalAmount(commodity.getCostPrice().multiply(new BigDecimal(storeStockDetail.getQuantity())));
        storeStockDetail.setInDate(new Date());
        storeStockDetail.setAfterQuantity(storeStockDetail.getBeforeQuantity() + storeStockDetail.getQuantity());
        StoreStock storeStock = StoreStock.builder()
                .commodityId(storeStockDetail.getCommodityId())
                .quantity(storeStockDetail.getAfterQuantity())
                .build();
        boolean b = storeStockService.updateQuantity(storeStock);
        if (b) {
            return save(storeStockDetail);
        }
        return false;
    }

    public QueryWrapper<StoreStockDetail> getQueryWrapper(StoreStockDetail storeStockDetail) {
        QueryWrapper<StoreStockDetail> queryWrapper = new QueryWrapper<>();
        if (storeStockDetail != null) {
            if (storeStockDetail.getCommodityId() != null) {
                queryWrapper.lambda().eq(StoreStockDetail::getCommodityId, storeStockDetail.getCommodityId());
            }
        }
        return queryWrapper;
    }

    /**
     * 字典翻译
     *
     * @param storeStockDetailList 对象集合
     */
    public void dictionary(List<StoreStockDetail> storeStockDetailList) {
        for (StoreStockDetail storeStockDetail : storeStockDetailList) {
            dictionary(storeStockDetail);
        }
    }

    public void dictionary(StoreStockDetail storeStockDetail) {
        if (storeStockDetail != null) {
            if (storeStockDetail.getCommodityId() != null) {
                Commodity commodity = commodityService.getById(storeStockDetail.getCommodityId());
                storeStockDetail.setCommodityName(commodity.getName());
            }
            if (storeStockDetail.getScene() != null) {
                storeStockDetail.setSceneName(dictService.findByTypeCode(DictUtil.STOCK_SCENE, storeStockDetail.getScene() + ""));
            }
        }
    }

}

